<template>
  <div>
    <i-dialog
      :title="dialogType === 'ADD' ? '批量添加' : '批量注销'"
      :visible="visible"
      width="642px"
      :close-on-click-modal="false"
      @close="outerClose"
      @open="outerOpen"
    >
      <div>
        <LoadFile v-model="uploadData" />
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="outerClose">取 消</el-button>
        <el-button type="primary" @click="submit">保存</el-button>
      </span>
    </i-dialog>
  </div>
</template>
<script>
import LoadFile from '@/components/LoadFile'
import { activateDevice, cancelDevice, cancelDeviceList } from '@/api/device'
export default {
  name: 'BatchUpload',
  components: { LoadFile },
  data() {
    return {
      uploadData: [],
    }
  },
  props: {
    visible: {
      type: Boolean,
      required: true,
    },
    dialogType: {
      type: String,
      default: 'ADD',
    },
    stationAttr: {
      type: Object,
      default: () => {},
    },
  },
  methods: {
    refreshTable() {
      this.$emit('refreshTable')
    },
    outerClose() {
      this.$emit('update:visible', false)
    },
    outerOpen() {},
    validElementLength(params) {
      for (var i, len = params.length; i < len; i++) {
        if (params[i].length > 20) {
          return false
        }
      }
      return true
    },
    handleBatchAdd() {
      let deviceIds = [],
        labels = [],
        socketNums = [],
        mchNames = []
      this.uploadData.forEach((item) => {
        item.DeviceID && deviceIds.push(item.DeviceID)
        item.mchName && mchNames.push(item.mchName)
        item.socketNum && socketNums.push(item.socketNum)
        item.Label &&
          labels.push(
            typeof item.Label === 'string' ? item.Label.trim() : item.Label
          )
      })
      if (!this.validElementLength(deviceIds)) {
        this.$message('数据错误,请检查导入的数据!')
        return
      }
      activateDevice({
        deviceIds: deviceIds,
        labels: labels,
        stationId: this.stationAttr.id,
        type: this.stationAttr.type,
        socketNum: socketNums,
        mchName: mchNames,
      }).then(
        (res) => {
          if (res.successNum === res.total) {
            this.$message({
              message: '批量添加设备成功',
              type: 'success',
            })
          } else {
            this.$message({
              message: res.fail.join(',') || '添加设备失败',
              type: 'error',
            })
          }
          this.outerClose()
          this.refreshTable()
        },
        () => {
          this.outerClose()
          this.refreshTable()
        }
      )
    },
    handleBatchLogout() {
      let deviceIds = []
      this.uploadData.forEach((item) => {
        item.DeviceID && deviceIds.push(item.DeviceID)
      })
      if (!this.validElementLength(deviceIds)) {
        this.$message('数据错误,请检查导入的数据!')
        return
      }
      cancelDevice({
        deviceIds: deviceIds,
      }).then(
        (res) => {
          if (res.successNum === res.total) {
            this.$message({
              message: '注销成功',
              type: 'success',
            })
          } else {
            this.$message({
              message: res.fail.join(',') || '注销设备失败',
              type: 'error',
            })
          }
          this.outerClose()
          this.refreshTable()
        },
        () => {
          this.outerClose()
          this.refreshTable()
        }
      )
    },
    handleAdminBatchLogout() {
      let deviceIds = []
      this.uploadData.forEach((item) => {
        item.DeviceID && deviceIds.push(item.DeviceID)
      })
      if (!this.validElementLength(deviceIds)) {
        this.$message('数据错误,请检查导入的数据!')
        return
      }
      cancelDeviceList({
        deviceIds: deviceIds,
      }).then(
        (res) => {
          if (res.successNum === res.total) {
            this.$message({
              message: '注销成功',
              type: 'success',
            })
          } else {
            this.$message({
              message: res.fail.join(',') || '注销设备失败',
              type: 'error',
            })
          }
          this.outerClose()
          this.refreshTable()
        },
        () => {
          this.outerClose()
          this.refreshTable()
        }
      )
    },
    submit() {
      // this.dialogType === 'ADD'
      //   ? this.handleBatchAdd()
      //   : this.handleBatchLogout()
      switch (this.dialogType) {
        case 'ADD':
          this.handleBatchAdd()
          break
        case 'LOGOUT':
          this.handleBatchLogout()
          break
        case 'ADMIN_LOGOUT':
          this.handleAdminBatchLogout()
          break
        default:
          break
      }
    },
  },
}
</script>
